package com.yhm.myfootbollshow.demos.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class OrderNumberGenerator {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
    private static String currentMinute = dateFormat.format(new Date());
    private static AtomicInteger counter = new AtomicInteger(1);

    public static synchronized String generateOrderNumber() {
        String now = dateFormat.format(new Date());
        if (!now.equals(currentMinute)) {
            currentMinute = now;
            counter.set(1);
        }
        return currentMinute + String.format("%04d", counter.getAndIncrement());
    }

    public static void main(String[] args) {
        // 测试生成订单号
        for (int i = 0; i < 10; i++) {
            System.out.println(generateOrderNumber());
            try {
                Thread.sleep(1000); // 模拟时间间隔
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
